﻿package j.sus2maj7.encoding
{
	import flash.utils.ByteArray;
	import j.sus2maj7.ability.*;
	import j.sus2maj7.context.*;
	
	public class AbstractEncoding extends AbstractDestroyable implements IEncoding
	{
		private var _binary:IBinary;
		private var _parsed:*;
		
		private var _codec:ICodec;
		
		public function AbstractEncoding()
		{
			
		}
		
		public function toString():String
		{
			return "";
		}
		
		//convenience
		public function setBytes(ba:ByteArray):void
		{
			binary = new AbstractBinary(ba);
		}
		
		public function get binary():IBinary { return _binary; }
		
		public function set binary(value:IBinary):void
		{
			_binary = value;
		}
		
		public function get object():*
		{
			//if (!object) decode();
			
			return _parsed;
		}
		
		public function set object(value:*):void
		{
			_parsed = value;
		}
		
		public function get codec():ICodec { return _codec; }
		
		public function set codec(value:ICodec):void
		{
			_codec = value;
		}
		
		public function decode(context:IContext=null):void
		{
			if (codec) this.object = codec.decode(binary, context);
		}
		
		public function encode(context:IContext=null):void
		{
			if (codec) this.binary = codec.encode(object, context);
		}
		
	}
	
}